package com.ikun.controller;

import com.aliyuncs.exceptions.ClientException;
import com.ikun.pojo.DeliveryAddress;
import com.ikun.pojo.Result;
import com.ikun.pojo.User;
import com.ikun.service.UserService;
import com.ikun.service.impl.UserServiceImpl;
import com.ikun.utils.JwtUtil;
import com.ikun.utils.UploadUtil;
import com.sun.tools.jconsole.JConsoleContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    UploadUtil uploadUtil;
    @Autowired
    UserService userService;
    /**
     * 根据username与password查询用户
     *
     */

    @GetMapping("/login")
    public Result getUserByUsernameAndPassword(
            @RequestParam("username") String username,
            @RequestParam("password") String password) {
        // 这里添加你的登录逻辑
        User user=userService.getUserByUsernameAndPassword(username, password);
        if (user!=null){
            // 生成jwt令牌
            Map<String, Object> claims = new HashMap<>();
            claims.put("userId", user.getUserId()); // 将用户ID添加到claims中
            String jwt = JwtUtil.generateJwt(claims);
            return Result.success(jwt);
        }else{
            return Result.error("用户名或密码不正确");
        }
    }
    //根据username与password添加用户
    @GetMapping("/register")
    public Result addUserByUsernameAndPassword(
            @RequestParam("username") String username,
            @RequestParam("password") String password) {
        // 这里添加你的添加用户的逻辑
        int result=userService.addUser(username, password);
        if (result>0){
            return Result.success(null);
        }else{
            return Result.error("注册失败");
        }
    }
    //根据用户id查询其收货地址
    @GetMapping("/getUserAddresses")
    public Result getAddressesByUserId(
            @RequestParam("userId") Integer userId) {
        return Result.success(userService.getAddressesByUserId(userId));
    }
    //添加收货地址
    @PostMapping("/addAddress")
    public Result addAddress(@RequestBody DeliveryAddress deliveryAddress) {
//        System.out.println("获取了数据");
//        System.out.println(deliveryAddress);
        int result=userService.addAddress(deliveryAddress);
        if(result>0){
            return Result.success();
        }else {
            return Result.error("添加收货地址失败");
        }

    }
    //修改收货地址
    @PutMapping("/updateAddress")
    public Result updateAddressByAddressId(@RequestBody DeliveryAddress deliveryAddress) {
        System.out.println("拿到了修改地址的数据");
        System.out.println(deliveryAddress);
        int result=userService.updateAddressByAddressId(deliveryAddress);
        if(result>0){
            return Result.success();
        }else {
            return Result.error("修改收货地址失败");
        }
    }
    //删除收货
    @DeleteMapping("/deleteAddress")
    public Result deleteAddressByAddressId(@RequestParam("addressId") Integer addressId) {
        int result=userService.deleteAddressByAddressId(addressId);
        if(result>0){
            return Result.success();
        } else {
            return Result.error("删除收货地址失败");
        }
    }
    @GetMapping("/getUserById/{id}")
    public Result getUserById(@PathVariable("id") Integer id){
        return Result.success(userService.getUserById(id));
    }

    @GetMapping("/updateUserNameByUserId/{id}")
    public Result updateUserNameByUserId(@PathVariable("id")Integer userId, @RequestParam(value = "userName") String userName){
        if (userName!=null) {
            // 如果为true，返回信息
            userService.updateUserNameByUserId(userId,userName);
            return Result.success();
        } else {
            // 如果为false或未指定，返回详细信息
            return Result.error("修改失败！");
        }
    }


    @GetMapping("/updateNameByUserId/{id}")
    public Result updateNameByUserId(@PathVariable("id")Integer userId, @RequestParam(value = "name") String name){
        if (name!=null) {
            // 如果为true，返回简介信息
            userService.updateNameByUserId(userId,name);
            return Result.success();
        } else {
            // 如果为false或未指定，返回详细信息
            return Result.error("修改失败！");
        }
    }

    @GetMapping("/updateBriefIntroductionByUserId/{id}")
    public Result updateBriefIntroductionByUserId(@PathVariable("id") Integer userId, @RequestParam(value = "briefIntroduction", defaultValue = "false") String briefIntroduction){

        if (briefIntroduction!=null) {
            // 如果briefIntroduction为true，返回简介信息
            userService.updateBriefIntroductionByUserId(userId,briefIntroduction);
            return Result.success();
        } else {
            // 如果briefIntroduction为false或未指定，返回详细信息
            return Result.error("修改失败！");
        }


    }
    @PostMapping("/uploadAvatar/{id}")
    public Result uploadAvatar(@PathVariable("id") Integer userId,@RequestParam MultipartFile image) throws ClientException, ClientException {
        System.out.println("获取到了图片"+image);
        System.out.println(userId);
        if (image!=null) {
            String resultUrl=uploadUtil.uploadFile(image);
            System.out.println("上传成功，路径为: "+resultUrl);
            userService.updateAvatar(userId,resultUrl);
            return Result.success();
        } else {
            return Result.error("修改失败！");
        }
    }
}
